<template>
	<view class="homeLayout pageBg">
		<!-- 兼容：抖音小程序需要一定的条件才允许自定义头部 -->
		#<!-- #ifndef MP-TOUTIAO -->
		<custom-nav-bar title="推荐"></custom-nav-bar>
		<!-- #endif -->
		<view class="banner">
			<swiper
			indicator-dots
			indicator-color="rgba(255,255,255,.5)"
			indicator-active-color="#fff"
			autoplay
			circular
			>
				<swiper-item v-for="item in bannerList" :key="item._id">
					<navigator
					v-if="item.target==='miniProgram'"
					:url="item.url"
					target="miniProgram"
					:app-id="item.appid"
					class="link">
						<image :src="item.picurl" mode="aspectFill"></image>
					</navigator>
					<navigator v-else :url="`/pages/classlist/classlist?${item.url}`" class="link">
						<image :src="item.picurl" mode="aspectFill"></image>
					</navigator>
				</swiper-item>
			</swiper>
		</view>
		
		<view class="notice">
			<view class="left">
				<uni-icons type="sound-filled" size="20"></uni-icons>
				<text class="text">公告</text>
			</view>
			<view class="center">
				<swiper vertical circular autoplay interval="1500" duration="300">
					<swiper-item v-for="item in noticeList" :key="item._id">
						<navigator :url="'/pages/notice/detail?id='+item._id">
							{{item.title}}
						</navigator>
					</swiper-item>
				</swiper>
			</view>
			<view class="right">
				<uni-icons type="right" size="20" color="#333"></uni-icons>
			</view>
		</view>
		
		<view class="select">
			<common-title>
				<template #title>每日推荐</template>
				<template #custom>
					<view class="date">
						<uni-icons type="calendar" size="18"></uni-icons>
						<view class="text">
							<uni-dateformat :date="Date.now()" format="dd日"></uni-dateformat>
						</view>
					</view>
				</template>
			</common-title>
			<view class="content">
				<scroll-view scroll-x>
					<!-- 这里跳转也可以用navigator，但会打乱布局，需要重新布局 -->
					<view class="box" v-for="item in randomList" :key="item._id" @click="goPreview(item._id)">
						<image :src="item.smallPicurl" mode="aspectFill"></image>
					</view>
				</scroll-view>
			</view>
		</view>
		
		<view class="theme">
			<common-title>
				<template #title>专题精选</template>
				<template #custom>
					<navigator url="/pages/classify/classify" open-type="reLaunch" class="more">More+</navigator>
				</template>
			</common-title>
			<view class="content">
				<theme-item v-for="item in classifyList" :key="item._id" :item="item"></theme-item>
				<theme-item :isMore="true"></theme-item>
			</view>
		</view>
		
		
		
	</view>
</template>

<script setup>
import { ref } from 'vue'
import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import { apiGetBanner, apiGetDayRandom, apiGetNotice, apiGetClassify } from '@/api/apis.js'

const bannerList = ref([])
const randomList = ref([])
const noticeList = ref([])
const classifyList = ref([])

// 获取轮播图数据
const getBanner = async () => {
	/* const res = await uni.request({
		url:"https://tea.qingnian8.com/api/bizhi/homeBanner"
	})
	// console.log(res)
	if (res.data.errCode === 0) {
		bannerList.value = res.data.data
	} */
	
	const res = await apiGetBanner()
	// console.log(res)
	bannerList.value = res.data
}

// 每日推荐 - 随机9张壁纸
const getDayRandom = async () => {
	/* const res = await uni.request({
		url:"https://tea.qingnian8.com/api/bizhi/randomWall"
	})
	console.log(res)
	if (res.data.errCode === 0) {
		randomList.value = res.data.data
	} */
	const res = await apiGetDayRandom()
	randomList.value = res.data
}

// 获取公告列表
const getNotice = async () => {
	/* const res = await uni.request({
		url:"https://tea.qingnian8.com/api/bizhi/wallNewsList"
	})
	console.log(res)
	if (res.data.errCode === 0) {
		noticeList.value = res.data.data
	} */
	
	const res = await apiGetNotice()
	// const res = await apiGetNotice({ select: true })
	// console.log(res)
	noticeList.value = res.data
}

// 专题精选获取分类
const getClassify = async () => {
	const res = await apiGetClassify({
		// pageSize: 3,
		select: true
	})
	// console.log(res)
	classifyList.value = res.data
}

// 跳转到预览页面
const goPreview = (id) => {
	uni.setStorageSync('storageClassList', randomList.value)
	uni.navigateTo({
		url: "/pages/preview/preview?id=" + id,
		// header: {
		// 	"access-key": "guest007"
		// }
	})
}

// 分享给好友
onShareAppMessage((e) => {
	// console.log(e)
	return {
		title: '咸虾米壁纸', // 标题
		path: 'pages/index/index' // 点击分享到哪个页面
	}
})

// 分享到朋友圈
onShareTimeline(() => {
	return {
		title: '咸虾米壁纸', // 标题
		// imageUrl: '/static/images/logo2.jpg' // 分享的链接的图片
	}
})

getBanner()
getDayRandom()
getNotice()
getClassify()
</script>

<style lang="scss" scoped>
.homeLayout {
	.banner {
		padding: 30rpx 0;
		width: 750rpx;
		swiper {
			width: 750rpx;
			height: 340rpx;
			swiper-item {
				padding: 0 30rpx;
				width: 100%;
				height: 100%;
				.link {
					width: 100%;
					height: 100%;
					image {
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}
			}
		}
	}

	.notice {
		display: flex;
		margin: 0 auto;
		// padding: 0 30rpx;
		width: 690rpx;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #f9f9f9;
		border-radius: 80rpx;
		.left {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 140rpx;
			:deep() {
				.uni-icons {
					color: $brand-theme-color !important;
				}
			}
			.text {
				color: $brand-theme-color;
				font-weight: 600;
				font-size: 28rpx;
			}
		}
		.center {
			flex: 1;
			swiper {
				height: 100%;
				&-item {
					height: 100%;
					font-size: 30rpx;
					color: #666;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
				}
			}
		}
		.right {
			width: 70rpx;
			text-align: center;
		}
	}

	.select {
		padding-top: 50rpx;
		.date {
			display: flex;
			align-items: center;
			color: $brand-theme-color;
			:deep() {
				.uni-icons {
					color: $brand-theme-color !important;
				}
			}
			.text {
				margin-left: 5rpx;
			}
		}
		.content {
			margin-top: 30rpx;
			margin-left: 30rpx;
			width: 720rpx;
			scroll-view {
				white-space: nowrap;
				// background-color: pink;
				// margin-right: 30rpx;
				.box {
					display: inline-block;
					margin-right: 15rpx;
					width: 200rpx;
					height: 430rpx;
					image {
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
					&:last-child {
						margin-right: 30rpx;
					}
				}
			}
		}
	}
	
	.theme {
		padding: 50rpx 0;
		.more {
			font-size: 30rpx;
			color: #888;
		}
		.content {
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 15rpx;
			margin-top: 30rpx;
			padding: 0 30rpx;
		}
	}

}
</style>
